using System;
using Slaks.DocumentSerialization.Document;
using Slaks.Graphs;
using Slaks.Graphs.GraphBuilder;

namespace Slaks.GraphExtensions.GraphBuilder
{
	/// <summary>
	/// Summary description for StandardRepresentationSlaks.
	/// </summary>
	public class StandardRepresentationExtendedSlaks : SlaksBuilder, IRepresentationExtended
	{
		#region IRepresentation Members

		public SuperGraph Build(Document document, int numberOfVerticesToLeave)
		{
			string documentName = document.FileName;
			int index = documentName.LastIndexOfAny(new char[] {'\\', '/'});
			if (index >= 0 && (index + 1 < documentName.Length)) documentName = documentName.Substring(index + 1);

			SuperGraph superGraph = new SuperGraph(documentName);

			SuperGraph tempSuperGraph = this.BuildTemporalSuperGraph(document);
			this.RemoveInfrequentVerticesInTemporalSuperGraph(tempSuperGraph, numberOfVerticesToLeave);

			Vertex vertex;
			string sectionType;

			Graph tempGraph;
			Graph graph;
			//iterate through nodes
			foreach (Node node in document.Nodes)
			{
				//iterate through sections
				foreach (Section section in node.Sections)
				{
					sectionType = section.GetSectionTypeString();


					//xml provides separate section for meta-description, but shenker algorithm treats meta-description as title
					if (sectionType == "meta-description")
						sectionType = "title";

					tempGraph = tempSuperGraph.GetGraphBySectionType(sectionType);
					if (tempGraph == null) continue;
					graph = superGraph.GetGraphBySectionType(sectionType);
					if (graph == null)
					{
						graph = new Graph(documentName);
						graph.Category = sectionType;
						superGraph.AddGraph(graph, sectionType);
					}

					Section.SectionType sectionTp = Section.GetSectionTypeByString(sectionType);

					//check if the vertex is in the list of frequent nodes.
					if (tempGraph.GetVertex(node.Id) == null) continue;
					vertex = graph.CreateAddVertex(node.Id);
					vertex.Frequency = section.TermFrequency;

					//iterate through sentences
					foreach (Sentence sentence in section.Sentences)
					{
						AddRelations(document, graph, tempGraph, vertex, 1, 1, sentence, sectionTp, BuilderType.StandardRepresentationSlaks);
					}
				}
			}


			return superGraph;
		}

		public SuperGraph Build(Document document, int numberOfVerticesToLeave, int nParameter)
		{
			throw new NotImplementedException("StandardRepresentationSlaks does not implement this method");
		}

		#endregion
	}
}